package com.xq.shop.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xq.shop.util.Const;
import com.xq.shop.vo.Operator;

public class AccessFilter implements Filter {

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        HttpSession session = request.getSession();
        Operator operator = (Operator)session.getAttribute(Const.Operator_Session);
        
        if( (operator == null || (operator != null && operator.getUser() == null))
        		&& request.getRequestURI().indexOf("login.jsp") == -1
        		&& request.getRequestURI().indexOf("admin/admin_login.do") == -1){
        	
        	response.sendRedirect(request.getContextPath() + "/jsp/admin/login.jsp");
        	return;
        } 
        
        System.out.println(request.getRequestURI());
//        if( operator != null && !operator.getPrivileges().contains(request.getRequestURI()) 
//        		&& request.getRequestURI().indexOf("admin/admin_logout.do") == -1
//        		&& request.getRequestURI().indexOf("login.jsp") == -1
//        		&& request.getRequestURI().indexOf("admin/admin_login.do") == -1
//        		&& request.getRequestURI().indexOf("jsp/admin/index.jsp") == -1){
//        	//TODO goto error page ,forbidden permission denied
//        	response.sendRedirect(request.getContextPath() + "/jsp/admin/login.jsp");
//        	return;
//        }
        filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {}

}
